package com.atom.common.cas;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: MyFilter <br/>
 * Function: redirect page and support CORS. <br/>
 *
 * @date: 2016-01-02 15:07:44 <br/>
 * @author Qiaozhen
 * @version 1.0
 * @since JDK 1.7
 */
public class MyFilter implements Filter {

  /*
   * (non-Javadoc)
   * 
   * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
   * javax.servlet.FilterChain)
   * 
   */
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept");

    HttpServletRequest request = (HttpServletRequest) req;
    String requestUri = request.getServletPath();
    // 主要用来将请求跳转到doRedirect.do?p=home
    /*
     * if(requestUri != null && (requestUri.startsWith("/user/tree.do") ||
     * requestUri.startsWith("/user/hruser.do") || requestUri.startsWith("/user/AuthGetOrg.do") ||
     * requestUri.startsWith("/user/AuthGetName.do"))){
     */
    request.getRequestDispatcher(requestUri).forward(request, response);
    return;
    /*
     * } chain.doFilter(request, response);
     */
  }

  public void init(FilterConfig filterConfig) {}

  public void destroy() {}

}
